//
//  XJStatusOriginalView.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/23.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit
import SnapKit
class XJStatusOriginalView: UIView {
    
    //记录当前原创微博的底部约束
    var originalViewBottomConstraint:Constraint?
    
    var statusViewModel: XJStatusViewModel?{
        didSet{
            headImageView.xj_setImage(statusViewModel?.status?.user?.profile_image_url, placeHolderImgName: "avatar_default")
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLabel.text = statusViewModel?.sinaTimeStr
            sourceLabel.attributedText = statusViewModel?.sourceStr
            memberImageView.image = statusViewModel?.memberImage
            avatarImageView.image = statusViewModel?.avatarImage
            contentLabel.text = statusViewModel?.status?.text
            //配图的显示与影藏
            //写在约束
            originalViewBottomConstraint?.uninstall()
            //有配图
            if statusViewModel?.status?.pic_urls?.count > 0{
                
                pictureView.pictureInfo = statusViewModel?.status?.pic_urls
                //更新
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                //显示配图
            pictureView.hidden = false
            }else{
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.hidden = true
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
     backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        contentLabel.preferredMaxLayoutWidth = SCREENW - 20
        contentLabel.numberOfLines = 0
        contentLabel.font = UIFont.systemFontOfSize(15)
        //设置约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSize(width:35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
        
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        avatarImageView.snp_makeConstraints { (make) -> Void in
         make.centerY.equalTo(headImageView.snp_bottom)
         make.centerX.equalTo(headImageView.snp_trailing)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(headImageView)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((SCREENW - (cellWH * 3 + cellMargin * 2))/2)
        }
//        给当前原创微博设置约束
        self.snp_makeConstraints { (make) -> Void in
//            make.bottom.equalTo(contentLabel).offset(10)
            self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    
    //懒加载控件
    
    private lazy var headImageView:UIImageView = UIImageView(imgName: "avatar_default")
    //昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
    //会员等级
    private lazy var memberImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    //时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), textFont: 10)
    
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), textFont: 10)
    //avatar_vgirl认证
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //原创微博
    private lazy var contentLabel: UILabel = UILabel()
    //配图
    private lazy var pictureView: XJStatusPictureView = XJStatusPictureView()
}





